package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.*;
import com.baitengsoft.patentapimonitor.api.model.param.SearchApplicantAnalysisParam;
import com.baitengsoft.patentapimonitor.api.model.param.WebMonitorYearsInfo;
import com.baitengsoft.patentapimonitor.api.service.*;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Chen 2018/1/19
 */
@RestController
@RequestMapping("/api/v1/web/home")
@Api(value = "前台-概况页", description = "概况页")
public class WebHomeController extends AbstractController {
    @Autowired
    private WebHomeService webHomeService;
    @Autowired
    private RptPatentDataAreaService rptPatentDataAreaService;
    @Autowired
    private RptApplicantService rptApplicantService;
    @Autowired
    private PacApplicantService applicantService;

    /**
     * 获取专利概况
     */
    @GetMapping("/pat")
    @ApiOperation(value = "专利概况")
    public Res getOverViewData() {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("用户信息获取失败");
        }
        //获取专利总量
        WebHomePatentSummaryInfo summaryInfo = webHomeService.getPatCountSum(userInfo.getPlatformid());
        WebBaseAnalysisInfo zlzlinfo = new WebBaseAnalysisInfo();
        zlzlinfo.setFmcount(summaryInfo.getFmcount());
        zlzlinfo.setSyxxcount(summaryInfo.getSyxxcount());
        zlzlinfo.setWgsjcount(summaryInfo.getWgsjcount());

        WebBaseAnalysisInfo yxzlinfo = new WebBaseAnalysisInfo();
        yxzlinfo.setFmcount(summaryInfo.getYxfmcount());
        yxzlinfo.setSyxxcount(summaryInfo.getYxsyxxcount());
        yxzlinfo.setWgsjcount(summaryInfo.getYxwgsjcount());

        WebBaseAnalysisInfo jnxzinfo = new WebBaseAnalysisInfo();
        jnxzinfo.setFmcount(summaryInfo.getYeargkfmcount());
        jnxzinfo.setSyxxcount(summaryInfo.getYeargksyxxcount());
        jnxzinfo.setWgsjcount(summaryInfo.getYeargkwgsjcount());

        WebBaseAnalysisInfo jnxzsqinfo = new WebBaseAnalysisInfo();
        jnxzsqinfo.setFmcount(summaryInfo.getYearsqfmcount());
        jnxzsqinfo.setSyxxcount(summaryInfo.getYearsqsyxxcount());
        jnxzsqinfo.setWgsjcount(summaryInfo.getYearsqwgsjcount());
//        //获取当前有效专利
//        WebBaseAnalysisInfo yxzlinfo = webHomeService.getCurrentYxcount(userInfo.getPlatformid());
//        //获取今年新增
//        WebBaseAnalysisInfo jnxzinfo = webHomeService.getXzcountYear(userInfo.getPlatformid());
//        //获取今年新增授权
//        WebBaseAnalysisInfo jnxzsqinfo = webHomeService.getSqcountYear(userInfo.getPlatformid());
        return Res.ok().put("zlzlinfo", zlzlinfo).put("yxzlinfo", yxzlinfo).put("jnxzinfo", jnxzinfo).put("jnxzsqinfo", jnxzsqinfo);

    }

    /**
     * 趋势分析 0.历年申请趋势，1.历年授权趋势
     */
    @GetMapping("/trend/{type}")
    @ApiOperation(value = "趋势分析 0.历年申请趋势，1.历年授权趋势")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "type", value = " 0.历年申请趋势，1.历年授权趋势", required = true)
    })
    public Res getTrendData(@PathVariable("type") Integer type) {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("用户信息获取失败");
        }
        if (type != 1 && type != 0)
            return Res.error("参数有误");
        List<WebYearStatisticInfo> infos = rptPatentDataAreaService.getTrendAnalysisListForHome(userInfo.getPlatformid(), type);
        return Res.ok().put("infos", infos);
    }

    /**
     * 权力人分析
     */
    @GetMapping("/applyperson/{dataTypeValue}")
    @ResponseBody
    @ApiOperation(value = "权力人分析 1当前有效量，2专利总量，3今年公开量，4今年授权量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "dataTypeValue", value = " 1.当前有效量，2.专利总量", required = true)
    })
    public Res getApplyperson(@PathVariable("dataTypeValue") Integer dataTypeValue) {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("用户信息获取失败");
        }
        if (dataTypeValue != 1 && dataTypeValue != 2)
            return Res.error("参数有误");
        List<WebApplicantAnalysisInfo> infos = webHomeService.getApplicantAnalysis(userInfo.getPlatformid(), dataTypeValue);
        return Res.ok().put("info", infos);
    }

    /**
     * 区域统计-有效专利
     */
    @GetMapping("/area")
    @ResponseBody
    @ApiOperation(value = "区域统计（有效专利）")
    public Res getArea() {
//        有961条专利数据权利人未归属到区域
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("用户信息获取失败");
        }
        //获取未归属区域的权力人
        int i = applicantService.selectCountWithOutArea(userInfo.getPlatformid());
        //获取有效专利区域分析
        List<WebAreaStatisticInfo> infos = rptPatentDataAreaService.getYXAreaAnalysis(userInfo.getPlatformid());
        return Res.ok().put("count", i).put("info", infos);
    }

    /**
     * 万人有效发明指标分析
     */
    @GetMapping("/wryx")
    @ResponseBody
    @ApiOperation(value = "万人有效发明指标分析")
    public Res getWryx() {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("用户信息获取失败");
        }
        //人口
        WebIndicatorInfo info = webHomeService.getHomeIndicatorInfo(userInfo.getPlatformid());
        //根据月份信息
        List<WebIndicatorMonthInfo> monthinfo = webHomeService.getIndicatorByMonth(userInfo.getPlatformid());
        return Res.ok().put("Indicator", info).put("monthinfo", monthinfo);
    }


    @GetMapping("/indexStat")
    @ApiOperation(value = "获取首页统计新")
    public Res getIndexStatisticsInfo() {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("用户信息获取失败");
        }
        WebStatisticsInfo info = webHomeService.getIndexStatisticsInfo(userInfo.getPlatformid());
        return Res.ok().put("info", info);
    }


    @GetMapping("/indexStatTotal")
    @ApiOperation(value = "获取首页统计新--所有统计")
    public Res getIndexStaticTotalInfo() {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("用户信息获取失败");
        }
        return Res.ok().put("infos", webHomeService.getIndexStatisticsTotal(userInfo.getPlatformid()));
    }


    /**
     * 专利监控
     */
    @GetMapping("/patMon")
    @ResponseBody
    @ApiOperation(value = "专利监控")
    public Res getMonitorMon() {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("用户信息获取失败");
        }
        //专利监控 最近7年转入转出
        List<WebMonitorYearsInfo> infos = webHomeService.getInByYearRange(userInfo.getPlatformid());
        //失效专利原因分析
        List<WebInvalidReasonInfo> expiryPatInfos = webHomeService.getInvalidReasonInfo(userInfo.getPlatformid());
        //获取失效专利总数
        int sum = webHomeService.getInvalidPatCount(userInfo.getPlatformid());
        return Res.ok().put("Moninfo", infos).put("expiryPatInfos", expiryPatInfos).put("sum", sum);
    }


    /**
     * 专利大户排名
     */
    @GetMapping("/rank/{type}")
    @ApiOperation(value = "专利大户排名/1.当前有效量 2.专利总量 3.今年公开量 4今年授权量")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "type", value = "1.当前有效量 2.专利总量 3.今年公开量 4今年授权量", required = true)
    })
    public Res getRank(@PathVariable("type") Integer type) {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("用户信息获取失败");
        }
        if (type == null || type > 4 || type < 1) {
            return Res.error("请求数据有误");
        }
        PageResult<WebApplicantInfo> applicantAnalysisList = rptApplicantService.getRank(userInfo.getPlatformid(), type);
        return Res.ok().put("infos", applicantAnalysisList);
    }
}
